package
{
    import com.questetra.bpms.air.controller.QBPMSEvent;
    import com.questetra.bpms.air.controller.QBPMSLib;
    import com.questetra.bpms.air.model.FormData;
    import com.questetra.bpms.air.model.Workitem;
    import com.questetra.bpms.air.model.WorkitemForm;
    
    import flash.events.MouseEvent;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;
    
    import mx.containers.TitleWindow;
    import mx.controls.Button;
    import mx.managers.PopUpManager;

    public class TaskInputWindow extends TitleWindow
    {
        public function TaskInputWindow()
        {
            super();
        }

        private var _workitem:Workitem;
        private var _workitemForm:WorkitemForm = new WorkitemForm();
        private var _qbpmsLib:QBPMSLib;
//        private var _inputObjects:Array /* of DisplayObject */ = [];

        public function init(data:Object, qbpmsLib:QBPMSLib):void
        {
            _qbpmsLib = qbpmsLib;
            this._workitem = data as Workitem;
            _qbpmsLib.qbpmsEventDispacher.addEventListener(QBPMSEvent.ON_TASK_INPUT_RECEIVED, onTaskInputReceived);
            _qbpmsLib.getTaskInput(_workitem.id);
        }

        private function onTaskInputReceived(event:QBPMSEvent):void
        {
            _workitemForm = event.data as WorkitemForm;
            this.title = _workitem.processModelInfoName + " : " + _workitemForm.taskName + " : " + _workitem.processInstanceTitle;

            for each (var formData:FormData in _workitemForm.formDatas)
            {
                this.addChild(FormDataManager.getInstance().createInputObject(formData));
            }

            var button:Button = new Button();
            button.label = "submit"
            button.addEventListener(MouseEvent.CLICK, onSubmit);
            this.addChild(button);
        }

        private function onSubmit(event:MouseEvent):void
        {
//TODO 本来ならここでURLVariablesを作成すべきでないが、FormDataManagerがviewの要素も持っているのでとりあえずこの形に
            var v:URLVariables = new URLVariables();
            for each (var formData:FormData in _workitemForm.formDatas)
            {
                v[FormDataManager.getParameterName(formData)] = FormDataManager.getInstance().getValue(formData);
            }

            _qbpmsLib.qbpmsEventDispacher.addEventListener(QBPMSEvent.ON_TASK_INPUT_RECEIVED, onTaskInputReceived);
            _qbpmsLib.executeTask(v, "AIRtest" + this._workitem.id, this._workitem.id);

            PopUpManager.removePopUp(this);
//TODO　タスクリスト更新まだ
        }
    }
}